import { Component, OnInit, ViewChild, OnDestroy, inject } from '@angular/core';
import { PageHeaderModule } from '@delon/abc/page-header';
import { STPage, STComponent, STColumn, STChange, STModule } from '@delon/abc/st';
import { DelonACLModule } from '@delon/acl';
import { DelonFormModule, SFSchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { Api } from 'src/app/shared/api/api';
import { BaseAbilityComponent } from 'src/app/shared/base.ability.component';
import { PLAT_SOURCE_ENUM, PLAT_SOURCE_TAG, DELETED_STATUS_ENUM, DELETED_STATUS_BADGE } from 'src/app/shared/constant/common-constant';
import { ResponseCode } from 'src/app/shared/response.code';

@Component({
  selector: 'app-system-user-third',
  templateUrl: './third-user.component.html',
  imports: [PageHeaderModule, DelonFormModule, STModule, NzCardModule, DelonACLModule]
})
export class ThirdUserComponent extends BaseAbilityComponent implements OnInit, OnDestroy {
  params: NzSafeAny = {};
  searchParams: NzSafeAny = {};
  page: NzSafeAny = {
    records: [],
    current: 1,
    total: 0,
    size: 10
  };
  pagination: STPage = {
    front: false,
    pageSizes: [10, 20, 30, 40, 50],
    total: true,
    showSize: true,
    showQuickJumper: true
  };

  searchSchema: SFSchema = {
    properties: {
      username: {
        type: 'string',
        title: '用户名',
        ui: {
          acl: { ability: ['query'] }
        }
      },
      userId: {
        type: 'string',
        title: '用户ID',
        ui: {
          acl: { ability: ['query'] }
        }
      },
      platId: {
        type: 'string',
        title: '平台ID',
        ui: {
          acl: { ability: ['query'] }
        }
      },
      unionId: {
        type: 'string',
        title: '平台统一ID',
        ui: {
          acl: { ability: ['query'] }
        }
      },
      platSource: {
        type: 'string',
        title: '平台类型',
        default: '',
        enum: PLAT_SOURCE_ENUM,
        ui: {
          widget: 'select',
          acl: { ability: ['query'] }
        }
      },
      deleted: {
        type: 'string',
        title: '状态',
        default: false,
        enum: DELETED_STATUS_ENUM,
        ui: {
          widget: 'select',
          acl: { ability: ['query'] }
        }
      }
    }
  };

  @ViewChild('st', { static: true }) st!: STComponent;
  columns: STColumn[] = [
    { title: '用户名', index: 'username' },
    { title: '用户ID', index: 'userId' },
    { title: '平台ID', index: 'platId' },
    { title: '平台统一ID', index: 'unionId' },
    { title: '平台类型', index: 'platSource', type: 'tag', tag: PLAT_SOURCE_TAG },
    { title: '状态', index: 'deleted', type: 'badge', badge: DELETED_STATUS_BADGE }
  ];
  protected readonly http = inject(_HttpClient);

  ngOnInit() {
    super.initAbilities();
    this.searchParams = { deleted: false };
    this.query();
  }
  ngOnDestroy(): void {
    super.clearAbilities();
  }

  change(e: STChange) {
    if (e.type === 'pi' || e.type === 'ps') {
      this.params.size = e.ps;
      this.params.current = e.pi;
      this.query();
    }
  }

  search(event: NzSafeAny) {
    if (event) {
      if (event.username) {
        this.searchParams.username = event.username;
      } else {
        delete this.searchParams.username;
      }
      if (event.userId) {
        this.searchParams.userId = event.userId;
      } else {
        delete this.searchParams.userId;
      }
      if (event.unionId) {
        this.searchParams.unionId = event.unionId;
      } else {
        delete this.searchParams.unionId;
      }
      if (event.platId) {
        this.searchParams.platId = event.platId;
      } else {
        delete this.searchParams.platId;
      }
      if (event.platSource) {
        this.searchParams.platSource = event.platSource;
      } else {
        delete this.searchParams.platSource;
      }
      if (event.deleted !== null) {
        this.searchParams.deleted = event.deleted;
      } else {
        delete this.searchParams.deleted;
      }
      this.query();
    }
  }

  query() {
    const current: number = this.params.current || 1;
    const size: number = this.params.size || 10;
    this.http.get(`${Api.BaseThirdUserApi}/page/${current}/${size}`, this.searchParams).subscribe((res: NzSafeAny) => {
      if (res && res.code === ResponseCode.SUCCESS) {
        if (res.data) this.page = res.data;
      }
    });
  }
}
